﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Media;

namespace teach
{
    public partial class AudioPanel : PanelBase
    {
        private string m_audioPath = null;
        private SoundPlayer simpleSound;
        private int count = 10;
        private bool timeouted = false;

        public AudioPanel()
        {
            InitializeComponent();
        }

        private float GetFont(string data)
        {
            float size = 36;
            if (data.Length > 100)
            {
                size = 20;
            }
            else if (data.Length > 75
                )
            {
                size = 22;
            }
            else if (data.Length > 45)
            {
                size = 24;
            }
            else if (data.Length > 20)
            {
                size = 32;
            }
            return size;
        }

        /// <summary>
        /// Set the operation question running
        /// </summary>
        /// <param name="question"></param>
        public override void SetQuestion(Question question)
        {
            Reset();
            m_answered = false;
            m_currentQuestion = question;
            txtAnswer.Text = "";
            this.timeoutMeter.Interval = 12000;
            this.timeoutMeter.Enabled = true;
            m_audioPath = Path.Combine(m_additionalResourcePath, question.Properties["audio"]);
            txtAnswer.Focus();
        }

        public override Control GetAnswerControl()
        {
            return txtAnswer;
        }

        public Button ButtonAnswer
        {
            get
            {
                return btnNext;
            }
        }

        public void StopAudio()
        {
            if (simpleSound!=null)
            {
                simpleSound.Stop();
            }
        }

        public override void SetStatusImage(bool success, Image image)
        {
            pctStatus.Visible = true;
            pctStatus.Image = image;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = true;
            if (success)
            {
                txtAnswer.BackColor = Color.LightBlue;
            }
            else
            {
                txtUserAnswer.Visible = true;
                txtAnswer.BackColor = Color.Tomato;
                txtAnswer.Text = m_currentQuestion.Answer.ToString();
            }
        }

        private void Reset()
        {
            timeouted = false;
            pctStatus.Visible = false;
            pctStatus.Image = null;
            pctStatus.Refresh();
            txtAnswer.ReadOnly = false;
            txtAnswer.ForeColor = Color.Black;
            txtAnswer.BackColor = Color.White;
            txtCounter.Visible = false;
            txtAnswer.Text = "";
            count = 10;
            txtUserAnswer.Visible = false;
            btnNext.Text = "Réponse";
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            m_answered = !m_answered;
            timeoutMeter.Enabled = false;
            txtCounter.Visible = false;
            if (m_answered)
            {
                if (txtAnswer.Text.Trim() == string.Empty && !timeouted)
                {
                    m_answered = !m_answered;
                    return;
                }
                txtUserAnswer.Text = "Vous aviez répondu:" + Environment.NewLine + Environment.NewLine + txtAnswer.Text;
                base.RaiseAnswer(txtAnswer.Text);
                btnNext.Text = "Prochain";
            }
            else
            {
                Reset();
                base.RaiseNextClicked();
            }
        }

        private void btnAudio_Click(object sender, EventArgs e)
        {
            simpleSound = new SoundPlayer(m_audioPath);
            simpleSound.Play();
            txtAnswer.Focus();
        }

        private void timeoutMeter_Tick(object sender, EventArgs e)
        {
            txtCounter.Text = count.ToString();
            timeoutMeter.Interval = 2000;
            txtCounter.Visible = true;
            if (count == 0)
            {
                timeouted = true;
                btnNext.PerformClick();
            }
            count--;
        }
    }
}
